// pages/community_worker/handle_to_unit.js
//获取应用实例
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabbar: {},
        theme: {},
        search: "",
        active: 0,
        index: 0,
        ConvenientList: [
            {title: '公用', id: 1},
            {title: '网格员', id: 2},
            {title: '社区民警', id: 4},
            {title: '社区值班电话', id: 5},
            {title: '物业值班电话', id: 3},
        ],
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        hidden: true,
        isLoading: true,
        namelist: [
            [], [], [], [], [], [], [], [], [], [], [], [], []
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.editTabBar();
        this.setData({
            theme: app.globalData.theme,
            tabbar: app.globalData.tabBar
        })
        let list = [];
        for (let i = 0; i < 26; i++) {
            list[i] = String.fromCharCode(65 + i)
        }
        this.setData({
            list: list,
            listCur: list[0]
        })
        this.getPhone()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        let that = this;
        wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function (res) {
            that.setData({
                boxTop: res.top
            })
        }).exec();
        wx.createSelectorQuery().select('.indexes').boundingClientRect(function (res) {
            that.setData({
                barTop: res.top
            })
        }).exec()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 跳转分类详情
     */
    getConvenient: function (event) {
        const id = event.currentTarget.dataset.id
        wx.navigateTo({
            url: '../community_worker/convenientdetails/convenientdetails?id=' + id
        })
    },
    onChange(event) {
        this.setData({
            index: event.detail.name
        })
        this.getPhone()
    },
    /**
     * 请求便民电话详情
     */
    getPhone: function () {
        const than = this
        this.setData({
            isLoading: true
        })
        let is = true
        http.get("phone/open/list", {
            name: than.data.search,
            type: than.data.index === 0 ? "" : than.data.index,
            area_code: app.globalData.areaCode
        }).then((res) => {
            if (res.data.code == 200) {
                res.data.data.forEach((item, index) => {
                    if (item.length != 0) {
                        is = false
                    }
                })
                if (!is) {
                    than.setData({
                        namelist: res.data.data
                    })
                } else {
                    than.setData({
                        namelist: []
                    })
                }
            }
            than.setData({
                isLoading: false
            })
        })
    },
    //获取文字信息
    getCur(e) {
        this.setData({
            hidden: false,
            listCur: this.data.list[e.target.id],
        })
    }, setCur(e) {
        this.setData({
            hidden: true,
            listCur: this.data.listCur
        })
    }, //滑动选择Item
    tMove(e) {
        let y = e.touches[0].clientY,
            offsettop = this.data.boxTop,
            that = this;
        //判断选择区域,只有在选择区才会生效
        if (y > offsettop) {
            let num = parseInt((y - offsettop) / 20);
            this.setData({
                listCur: that.data.list[num]
            })
        }
        ;
    },

    //触发全部开始选择
    tStart() {
        this.setData({
            hidden: false
        })
    },

    //触发结束选择
    tEnd() {
        this.setData({
            hidden: true,
            listCurID: this.data.listCur
        })
    },
    indexSelect(e) {
        let that = this;
        let barHeight = this.data.barHeight;
        let list = this.data.list;
        let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
        for (let i = 0; i < list.length; i++) {
            if (scrollY < i + 1) {
                that.setData({
                    listCur: list[i],
                    movableY: i * 20
                })
                return false
            }
        }
    },
    /**
     * 电话呼叫
     */
    getPhonenum: function (event) {
        const phone = event.currentTarget.dataset.phone
        Dialog.confirm({
            title: '提示',
            message: '确认拨打' + phone + '吗？',
        }).then(() => {
            wx.makePhoneCall({
                phoneNumber: phone
            })
        }).catch(() => {
            // on cancel
        });
    },
    /**
     * 实时监听搜索输入框
     * @param e
     */
    onInputSearch: function (e) {
        this.setData({
            search: e.detail.value
        })
        this.getPhone();
    },
    /**
     * 搜索框 键盘确定按钮点击
     * @param e
     */
    onSearchConfirm: function (e) {
        log.e(JSON.stringify(e))
        this.setData({
            search: e.detail.value
        })
        this.getPhone()
    },
})